package com.yudaob2b.elong.service;

import com.yudaob2b.business.entity.po.ElongHotelInventoryPO;
import com.yudaob2b.business.entity.po.ElongHotelRatePO;
import com.yudaob2b.common.utils.DateUtil;
import com.yudaob2b.elong.constant.RedisConstant;
import com.yudaob2b.elong.entity.dto.ELaElongHotelRateDTO;
import com.yudaob2b.elong.entity.vo.Inventory;
import com.yudaob2b.elong.entity.vo.Rate;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 价格数据服务
 *
 * @author Kidd.Tan
 * @email 298023780@qq.com
 * @date 7/17/18 11:56 PM
 */
@Service("hotelRateService")
public class HotelRateService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * Rate转换ElongHotelRatePO
     * @param rate
     * @return
     */
    public ElongHotelRatePO getPoByRate(Rate rate) {
        ElongHotelRatePO po = new ElongHotelRatePO();
        try {
            BeanUtils.copyProperties(rate, po);
            po.setHotelId(rate.getHotelID());
            po.setRoomTypeId(rate.getRoomTypeId());
            po.setRatePlanId(String.valueOf(rate.getRateplanId()));
            if(rate.isStatus()) {
                po.setStatus(1);
            }else {
                po.setStatus(0);
            }
            po.setPriceId(rate.getPriceID());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return po;
    }

    /**
     * Rate转换ElongHotelRatePO
     * @param rate
     * @return
     */
    public ELaElongHotelRateDTO getElaDtoByRate(Rate rate) {
        ELaElongHotelRateDTO dto = new ELaElongHotelRateDTO();
        try {
            BeanUtils.copyProperties(rate, dto);
            StringBuilder idBuilder = new StringBuilder();
            idBuilder.append(rate.getHotelID()).append("_")
                    .append(rate.getRoomTypeId()).append("_")
                    .append(rate.getRateplanId()).append("_")
                    .append(rate.getHotelCode()).append("_")
                    .append(DateUtil.format(rate.getEndDate(), DateUtil.PATTERN_CLASSICAL_SIMPLE));
            dto.setId(idBuilder.toString());
            dto.setUpdateTime(new Date());
            dto.setHotelId(rate.getHotelID());
            dto.setRoomTypeId(rate.getRoomTypeId());
            dto.setRatePlanId(String.valueOf(rate.getRateplanId()));
            if(rate.isStatus()) {
                dto.setStatus(1);
            }else {
                dto.setStatus(0);
            }
            dto.setPriceId(rate.getPriceID());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return dto;
    }

    /**
     * 缓存库存增量参数
     *
     * @param incrRateLastId 最后更新id
     * @param updateTime    最后更新时间
     */
    public void updateIncrLastIdInfo(String incrRateLastId, Date updateTime) {
        redisTemplate.opsForValue().set(RedisConstant.REDIS_INCR_RATE_LASTID_KEY, incrRateLastId);
        redisTemplate.opsForValue().set(RedisConstant.REDIS_INCR_RATE_UPDATE_TIME_KEY, updateTime);
    }
}
